home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Mousebroken 1.0.1 / source / Mousebroken source ƒ / cdev code / cdev meat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  9.0 KB  |  308 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        cdev meat.c
  4.  
  5. Purpose:    This module handles stuff internal to the cdev -- checking
  6.             the right items and handling buttons.  Also installing &
  7.             de-installing the actual mouse VBL.
  8.             
  9.  
  10. Mousebroken -=- your computer isn't truly broken until it's mousebroken
  11. Copyright (C) 1993 Mark Pilgrim
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program in a file named "GNU General Public License".
  25. If not, write to the Free Software Foundation, 675 Mass Ave,
  26. Cambridge, MA 02139, USA.
  27.  
  28. \**********************************************************************/
  29.  
  30. #include "cdev globals.h"
  31. #include "cdev meat.h"
  32. #include "cdev prefs.h"
  33. #include "cdev.h"
  34. #include "Retrace.h"
  35. #include "Folders.h"
  36. #include "GestaltEQU.h"
  37.  
  38. #define MODULE_TYPE        'MMdl'
  39. #define MODULE_CREATOR    'MBrk'
  40. #define MODULE_FOLDER    "\pMouse Modules"
  41.  
  42. void RemoveTheMouseVBL(PrefHandle cdevStorage)
  43. {
  44.     if (((**cdevStorage).moduleIndex!=0) &&
  45.         ((**cdevStorage).mouseVBLPtr>=(unsigned long)RAMBase) &&
  46.         ((**cdevStorage).mouseVBLPtr<(unsigned long)MemTop) &&
  47.         ((**cdevStorage).mouseCodePtr>=(unsigned long)RAMBase) &&
  48.         ((**cdevStorage).mouseCodePtr<(unsigned long)MemTop) &&
  49.         (GetPtrSize((Ptr)((**cdevStorage).mouseVBLPtr))==0x0e) &&
  50.         (VRemove((VBLTask*)((**cdevStorage).mouseVBLPtr))==noErr))
  51.     {
  52.         DisposePtr((Ptr)((**cdevStorage).mouseVBLPtr));
  53.         DisposeHandle((Handle)((**cdevStorage).mouseCodePtr));
  54.     }
  55. }
  56.  
  57. int InstallTheMouseVBL(PrefHandle cdevStorage)
  58. {
  59.     VBLTask            *vblPtr;
  60.     Handle            ourCode;
  61.     int                oldResFile;
  62.     
  63.     oldResFile=CurResFile();
  64.     UseResFile((**cdevStorage).moduleRefNum);
  65.     ourCode=0L;
  66.     ourCode=GetResource('vbl ', 668);
  67.     UseResFile(oldResFile);
  68.     if (ourCode!=0L)
  69.     {
  70.         if (*ourCode==0L)
  71.             LoadResource(ourCode);
  72.         if (*ourCode==0L)
  73.             return kCantGetResource;
  74.         
  75.         HLock(ourCode);
  76.         DetachResource(ourCode);
  77.         
  78.         vblPtr = (VBLTask*) NewPtrSys(sizeof(VBLTask));
  79.         vblPtr->qType = vType;
  80.         vblPtr->vblAddr = (ProcPtr) *ourCode;
  81.         vblPtr->vblCount = 1;
  82.         vblPtr->vblPhase = 0;
  83.         VInstall(vblPtr);
  84.         (**cdevStorage).mouseCodePtr=(unsigned long)ourCode;
  85.         (**cdevStorage).mouseVBLPtr=(unsigned long)vblPtr;
  86.     }
  87.     else return kCantGetResource;
  88.  
  89.     return prefs_allsWell;
  90. }
  91.  
  92. int GetModuleInfo(PrefHandle cdevStorage)
  93. {
  94.     OSErr            isHuman;
  95.     int                vRefNum;
  96.     long            dirID;
  97.     CInfoPBRec        pb_dir;
  98.     
  99.     if ((**cdevStorage).moduleIndex==0)
  100.         return GetModuleInfoFromIndex(cdevStorage, 1);
  101.     
  102.     isHuman=FindFolder(kOnSystemDisk, kSystemFolderType, kDontCreateFolder, &vRefNum,
  103.                 &dirID);                    /* find system folder */
  104.     if (isHuman!=noErr)
  105.         return kCantFindSystemFolder;
  106.     
  107.     pb_dir.dirInfo.ioCompletion=0L;
  108.     pb_dir.dirInfo.ioNamePtr=MODULE_FOLDER;
  109.     pb_dir.dirInfo.ioVRefNum=vRefNum;
  110.     pb_dir.dirInfo.ioFDirIndex=0;             /* very important */
  111.     pb_dir.dirInfo.ioDrDirID=dirID;
  112.     isHuman=PBGetCatInfo(&pb_dir, FALSE);    /* get info on "Mouse Modules" dir */
  113.     if (isHuman!=noErr)
  114.         return kCantOpenModuleFolder;
  115.  
  116.     if (!AtLeastOneModule(vRefNum, pb_dir.dirInfo.ioDrDirID))
  117.         return kNoModules;                    /* check if there are any modules at all */
  118.     
  119.     return FindCurrentModule(cdevStorage, vRefNum, pb_dir.dirInfo.ioDrDirID,
  120.             pb_dir.dirInfo.ioDrNmFls);
  121. }
  122.  
  123. int GetModuleInfoFromIndex(PrefHandle cdevStorage, int direction)
  124. {
  125.     OSErr            isHuman;
  126.     int                vRefNum;
  127.     long            dirID;
  128.     CInfoPBRec        pb_dir;
  129.     HParamBlockRec    pb_basic;
  130.     Str63            theName;
  131.     int                i;
  132.     int                tempIndex;
  133.     int                resultCode;
  134.     
  135.     isHuman=FindFolder(kOnSystemDisk, kSystemFolderType, kDontCreateFolder, &vRefNum,
  136.                 &dirID);                    /* find system folder */
  137.     if (isHuman!=noErr)
  138.         return kCantFindSystemFolder;
  139.     
  140.     pb_dir.dirInfo.ioCompletion=0L;
  141.     pb_dir.dirInfo.ioNamePtr=MODULE_FOLDER;
  142.     pb_dir.dirInfo.ioVRefNum=vRefNum;
  143.     pb_dir.dirInfo.ioFDirIndex=0; /* very important */
  144.     pb_dir.dirInfo.ioDrDirID=dirID;
  145.     isHuman=PBGetCatInfo(&pb_dir, FALSE);    /* get info on "Mouse Modules" dir */
  146.     if (isHuman!=noErr)
  147.         return kCantOpenModuleFolder;
  148.  
  149.     if (!AtLeastOneModule(vRefNum, pb_dir.dirInfo.ioDrDirID))
  150.         return kNoModules;                    /* check if there are any modules at all */
  151.     
  152.     if ((**cdevStorage).moduleIndex>0)
  153.     {
  154.         resultCode=FindCurrentModule(cdevStorage, vRefNum, pb_dir.dirInfo.ioDrDirID,
  155.                     pb_dir.dirInfo.ioDrNmFls);    /* get index # of current module */
  156.         if (resultCode!=allsWell)
  157.             return resultCode;
  158.     }
  159.     
  160.     tempIndex=(**cdevStorage).moduleIndex;
  161.     do
  162.     {
  163.         tempIndex+=direction;
  164.         if (tempIndex==0)
  165.             tempIndex=pb_dir.dirInfo.ioDrNmFls;
  166.         else if (tempIndex>pb_dir.dirInfo.ioDrNmFls)
  167.             tempIndex=1;
  168.         
  169.         pb_basic.fileParam.ioCompletion=0L;
  170.         pb_basic.fileParam.ioNamePtr=theName;
  171.         pb_basic.fileParam.ioVRefNum=vRefNum;
  172.         pb_basic.fileParam.ioFDirIndex=tempIndex;
  173.         pb_basic.fileParam.ioDirID=pb_dir.dirInfo.ioDrDirID;
  174.         isHuman=PBGetCatInfo(&pb_basic, FALSE);    /* get info on previous/next module */
  175.         if (isHuman!=noErr)
  176.             return kCantOpenModule;
  177.     }
  178.     while ((pb_basic.fileParam.ioFlFndrInfo.fdType!=MODULE_TYPE) ||
  179.             (pb_basic.fileParam.ioFlFndrInfo.fdCreator!=MODULE_CREATOR));
  180.     
  181.     (**cdevStorage).moduleIndex=tempIndex;
  182.     (**cdevStorage).moduleFS.vRefNum=vRefNum;
  183.     (**cdevStorage).moduleFS.parID=pb_dir.dirInfo.ioDrDirID;
  184.         for (i=theName[0]; i>=0; i--)
  185.         (**cdevStorage).moduleFS.name[i]=theName[i];
  186.     return allsWell;
  187. }
  188.  
  189. Boolean AtLeastOneModule(int vRefNum, long dirID)
  190. {
  191.     HParamBlockRec        pb_basic;
  192.     int                    dirIter;
  193.     Boolean                foundModule;
  194.     Str63                theName;
  195.     OSErr                isHuman;
  196.     
  197.     dirIter=0;
  198.     do
  199.     {
  200.         dirIter++;
  201.         pb_basic.fileParam.ioCompletion=0L;
  202.         pb_basic.fileParam.ioNamePtr=theName;
  203.         pb_basic.fileParam.ioVRefNum=vRefNum;
  204.         pb_basic.fileParam.ioFDirIndex=dirIter;
  205.         pb_basic.fileParam.ioDirID=dirID;
  206.         isHuman=PBGetCatInfo(&pb_basic, FALSE);
  207.         if (isHuman!=noErr)
  208.             return FALSE;
  209.         foundModule=(pb_basic.fileParam.ioFlFndrInfo.fdType==MODULE_TYPE) &&
  210.             (pb_basic.fileParam.ioFlFndrInfo.fdCreator==MODULE_CREATOR);
  211.     }
  212.     while ((isHuman==noErr) && (!foundModule));
  213.     
  214.     return (isHuman==noErr);
  215. }
  216.  
  217. int FindCurrentModule(PrefHandle cdevStorage, int vRefNum, long dirID, int numFiles)
  218. {
  219.     int                tempIndex;
  220.     Boolean            isCorrectModule;
  221.     HParamBlockRec    pb_basic;
  222.     Str63            theName;
  223.     OSErr            isHuman;
  224.     int                i;
  225.     
  226.     isCorrectModule=FALSE;
  227.     for (tempIndex=1; (tempIndex<=numFiles) && (!isCorrectModule); tempIndex++)
  228.     {
  229.         pb_basic.fileParam.ioCompletion=0L;
  230.         pb_basic.fileParam.ioNamePtr=theName;
  231.         pb_basic.fileParam.ioVRefNum=vRefNum;
  232.         pb_basic.fileParam.ioFDirIndex=tempIndex;
  233.         pb_basic.fileParam.ioDirID=dirID;
  234.         isHuman=PBGetCatInfo(&pb_basic, FALSE);    /* get info on current module */
  235.         if (isHuman!=noErr)
  236.             return kCantOpenModule;
  237.         isCorrectModule=(pb_basic.fileParam.ioFlFndrInfo.fdType==MODULE_TYPE) &&
  238.             (pb_basic.fileParam.ioFlFndrInfo.fdCreator==MODULE_CREATOR);
  239.         for (i=(**cdevStorage).moduleFS.name[0]; (i>=0) && (isCorrectModule); i--)
  240.             isCorrectModule=((**cdevStorage).moduleFS.name[i]==theName[i]);
  241.     }
  242.     
  243.     if (!isCorrectModule)
  244.     {
  245.         (**cdevStorage).moduleIndex=0;
  246.         return GetModuleInfoFromIndex(cdevStorage, 1);
  247.     }
  248.  
  249.     (**cdevStorage).moduleIndex=tempIndex-1;
  250.     (**cdevStorage).moduleFS.vRefNum=vRefNum;
  251.     (**cdevStorage).moduleFS.parID=dirID;
  252.     
  253.     return allsWell;
  254. }
  255.  
  256. int OpenTheModule(PrefHandle cdevStorage)
  257. {
  258.     unsigned long        oldTopMapHndl;
  259.     OSErr                theResError;
  260.     OSErr                isHuman;
  261.     unsigned long        gestalt_temp;
  262.     THz                    saveZone;
  263.     
  264.     (**cdevStorage).oldRefNum=CurResFile();
  265.     oldTopMapHndl=(unsigned long)TopMapHndl;
  266.     (**cdevStorage).moduleRefNum=HOpenResFile((**cdevStorage).moduleFS.vRefNum,
  267.         (**cdevStorage).moduleFS.parID, (**cdevStorage).moduleFS.name, fsRdPerm);
  268.     theResError=ResError();
  269.     (**cdevStorage).moduleAlreadyOpen=(oldTopMapHndl==(unsigned long)TopMapHndl);
  270.     if (theResError==noErr)
  271.     {
  272.         UseResFile((**cdevStorage).moduleRefNum);
  273.         isHuman = Gestalt(gestaltQuickdrawVersion, &gestalt_temp);
  274.         (**cdevStorage).moduleIconHandle=0L;
  275.         saveZone=GetZone();
  276.         SetZone(SysZone);
  277.         (**cdevStorage).moduleIconHandle=(isHuman || (gestalt_temp < gestalt8BitQD)) ?
  278.             (unsigned long)GetIcon(668) : (unsigned long)GetCIcon(668);
  279.         if ((**cdevStorage).moduleIconHandle!=0L)
  280.         {
  281.             HLock((Handle)(**cdevStorage).moduleIconHandle);
  282.             HNoPurge((Handle)(**cdevStorage).moduleIconHandle);
  283.         }
  284.         SetZone(saveZone);
  285.     }
  286.     
  287.     return (theResError==noErr) ? allsWell : kCantOpenModule;
  288. }
  289.  
  290. int CloseTheModule(PrefHandle cdevStorage)
  291. {
  292.     OSErr                isHuman;
  293.     unsigned long        gestalt_temp;
  294.     THz                    saveZone;
  295.     
  296.     if (!((**cdevStorage).moduleAlreadyOpen))
  297.         CloseResFile((**cdevStorage).moduleRefNum);
  298.     isHuman = Gestalt(gestaltQuickdrawVersion, &gestalt_temp);
  299.     saveZone=GetZone();
  300.     SetZone(SysZone);
  301.     if (isHuman || (gestalt_temp < gestalt8BitQD))
  302.         ReleaseResource((Handle)(**cdevStorage).moduleIconHandle);
  303.     else
  304.         DisposCIcon((CIconHandle)(**cdevStorage).moduleIconHandle);
  305.     SetZone(saveZone);
  306.     UseResFile((**cdevStorage).oldRefNum);
  307. }
  308.